﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.SiteMap;

/// <summary>
/// The purpose of this user control is to provide the
/// user a historical trail of links (crumbs) when navigating from one
/// page to the other.
/// </summary>
public partial class App_Common_UserControls_CrumbNavigation : System.Web.UI.UserControl
{
    /// <summary>
    /// When the page is first loaded we need to load up
    /// all the crumbs.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadCrumbs();
    }

    #region Crumb Management

    /// <summary>
    /// This routine looks up the current page and loads any 
    /// previous (parent) pages that might exist.
    /// </summary>
    private void LoadCrumbs()
    {
        string crumb = "";
        PageInfo pageInfo = Asah.SiteWorx.Site.GetSite().CurrentPage;

        if (pageInfo != null)
        {
            if (!pageInfo.IsHomePage && pageInfo.ParentPageId > -1)
            {
                LoadParentCrumbs(pageInfo.ParentPageId, ref crumb);
                crumb += pageInfo.MenuText;
                litCrumbNavigation.Text = crumb;
            }
        }

        if (crumb.Length > 0)
            pnlCrumb.Visible = true;
        else
            pnlCrumb.Visible = false;
    }

    /// <summary>
    /// This routine loads any previous (parent) pages that 
    /// might exist.
    /// </summary>
    /// <param name="parentPageId"></param>
    /// <param name="crumb"></param>
    private void LoadParentCrumbs(int parentPageId, ref string crumb)
    {
        PageInfo pageInfo = PageList.GetPageInfo(parentPageId);
        if (pageInfo != null)
        {
            crumb = "<a href=\"" + pageInfo.Url.Replace("~/", "").ToString() + "\">" +
                    pageInfo.MenuText + "</a>&nbsp;>&nbsp;" + crumb;
            if (pageInfo.ParentPageId > -1)
                LoadParentCrumbs(pageInfo.ParentPageId, ref crumb);

        }

    }

    /// <summary>
    /// The purpose of this routine is to allow an external
    /// control (object) the ability to append a custom
    /// crumb (link) item to our crumb navigation control.
    /// </summary>
    /// <param name="displayText"></param>
    /// <param name="url"></param>
    public void AppendCrumb(string displayText, string url)
    {
        if (displayText.Length > 0)
        {
            if (litCrumbNavigation.Text.Length == 0)
                litCrumbNavigation.Text = "<a href=\"default.aspx\">Home</a>";

            litCrumbNavigation.Text += "&nbsp;>&nbsp;";

            if (url.Length > 0)
                litCrumbNavigation.Text += "<a href=\"" + url.Replace("~/", "").ToString() + "\">" + displayText + "</a>";
            else
                litCrumbNavigation.Text += displayText.ToString();

            if (litCrumbNavigation.Text.Length > 0)
                pnlCrumb.Visible = true;

        }
    }

    #endregion
}